<?php

/**
 * Framework_Object
 *
 * @author      Joe Stump <joe@joestump.net>
 * @copyright   (c) 2006 Joseph C. Stump. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Framework
 * @filesource
 */

require_once 'Log.php';

/**
 * Framework_Object
 *
 * The base object class for most of the classes that we use in our framework.
 * Provides basic logging and set/get functionality.
 *
 * @author Joe Stump <joe@joestump.net>
 * @package Framework
 */
abstract class Framework_Object
{
    /**
     * $log
     *
     * @var mixed $log Instance of PEAR Log 
     */
    public $log;

    /**
     * $me
     *
     * @var mixed $me Instance of ReflectionClass
     */
    public $me;
    
    /**
     * $isCacheable
     *
     * @var boolean $isCacheable whether the object can be cached
     */
    public $isCacheable = false;
    
    /**
     * $cacheHandler
     *
     * @var Framework_Cache_Handler_Common $cacheHandler cache handler object
     */
    public $cacheHandler;
    
    /**
     * $cacheHandlerClass
     *
     * @var class $cacheHandlerClass Framework_Cache_Handler class
     */
    public $cacheHandlerClass = '';

    /**
     * __construct
     * 
     * @author Joe Stump <joe@joestump.net>
     * @access public 
     */
    public function __construct()
    {
        $this->log = self::createLog();
        $this->me = new ReflectionClass($this);
    }
    
    /**
     * getCacheHandler
     * 
     * @author 		Joe Stump <joe@joestump.net>
     * @access 		public 
     * @return 		mixed		PEAR::error on failure, Framework_Cache_Handler_Common object on success
     */
    public function getCacheHandler(){
    	
    	if (isset($this->cacheHandler)) {
    		return $this->cacheHandler;
    	} else if (strlen($this->cacheHandlerClass)){
    		$this->cacheHandler = Framework_Cache::handlerFactory($this->cacheHandlerClass, $this);
    		return $this->cacheHandler;
    	} else {
    		return PEAR::raiseError('Invalid Cache Handler');
    	}
    }

    /**
     * setFrom
     *
     * Takes the object's class vars and sets any found in $data to the value
     * found in $data. If Class::$foo exists and $data['foo'] is set to 'bar'
     * then your instance of Class's $foo will be set to 'bar'.
     *
     * @author Joe Stump <joe@joestump.net>
     * @access public
     * @param mixed $data Array of variables to assign to instance
     * @return void
     */
    public function setFrom($data)
    {
        if (is_array($data) && count($data)) {
            $valid = get_class_vars(get_class($this));
            foreach ($valid as $var => $val) {
                if (isset($data[$var])) {
                    $this->$var = $data[$var];
                }
            }
        }
    }

    /**
     * toArray
     *
     * Return the object's member variables as an associative array.
     *
     * @access public
     * @return mixed Array of member variables keyed by variable name
     */
    public function toArray()
    {
        $defaults = $this->me->getDefaultProperties();
        $return = array();
        foreach ($defaults as $var => $val) {
            if ($this->$var instanceof Framework_Object) {
                $return[$var] = $this->$var->toArray();
            } else {
                $return[$var] = $this->$var;
            }
        }

        return $return;
    }

    /**
     * createLog
     *
     * @access  private
     * @return  reference 
     * @static
     */
    static private function createLog()
    {
        if (is_null(Framework::$log)) {
            $logFile = (string)Framework::$site->config->logFile;
            Framework::$log = Log::factory('file', Framework::$site->getPath().$logFile);
        }

        return Framework::$log;
		 
    }

    /**
     * __sleep
     *
     * @access  public
     * @return  void
     */
    public function __sleep()
    {
        $this->log = null;
        
        
    }

    /**
     * __wakeup
     *
     * @access  public
     * @return  void
     */
    public function __wakeup()
    {
        $this->log = & self::createLog();
    }

    /**
     * __destruct
     *
     * @access public
     * @return void
     */
    public function __destruct()
    {
        if ($this->log instanceof Log) {
            $this->log->close();
        }
    }
}

?>
